TOP

VBA function: Choose

Description

The VBA Choose function returns a value from its argument list with a given number.


Choose syntax

Choose(number, value_1, value_2, value_3, etc.)

VBA Choose example

Display of one of 3 values by the number written in the "choice" variable:

  1. Sub ChooseExample1()  
  2.       
  3.       choice = 1  
  4.       
  5.       course = Choose(choice, "Excel""VBA""Google Sheets")  
  6.       
  7.       MsgBox "Selected course: " & course 'Returns the message: "Selected course: Excel"  
  8.       
  9. End Sub  

An alternative to the Choose function is to use an array (Array):

  1. Sub ChooseExample2()  
  2.       
  3.       choice = 1  
  4.       
  5.       course = Array("Excel""VBA""Google Sheets"))(choice)  
  6.       
  7.       MsgBox "Selected course: " & course 'Returns the message: "Selected course: VBA"  
  8.       
  9. End Sub  
The first value of the Choose function is at position 1, unlike the array, whose first value is at 0.